package com.open.party.cloud.view.freePhoto

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.FreePhotosBean
import com.open.party.cloud.model.FreePhotosFileBean
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.freePhoto.adpater.FreePhotosNineGridAdapter
import com.open.party.cloud.viewModel.FreePhotosViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.image.selector.PhotoPreviewActivity
import kotlinx.android.synthetic.main.free_photos_deal_with_detail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

class FreePhotosDetailActivity : AppEventBusTitleBaseActivity() {
    var viewModel: FreePhotosViewModel? = null
    var id = ""
    var fromWhere = 1 // 参数1，我发布的；2，待我处理；3，我已处理

    override fun getLayoutResId(): Int = R.layout.free_photos_deal_with_detail

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("随手拍处理")

        id = intent.getStringExtra("id") as String
        fromWhere = intent.getIntExtra("fromWhere", 1)
        viewModel = FreePhotosViewModel()
        showLoadingDialog("正在加载")
    }

    override fun onResume() {
        super.onResume()
        viewModel!!.getFreePhotosDetailById(id)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<FreePhotosBean>) {
        if (result.eventType != "getFreePhotosDetailById") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                showDetail(result.data)
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showDetail(freePhotosBean: FreePhotosBean) {
        userNameTv.text = XUtils.string().getNotNullValue(freePhotosBean.userName, "匿名")
        timeTv.text = XUtils.string().getNotNullValue(freePhotosBean.phone, "无联系方式")
        contentTv.text = XUtils.string().getNotNullValue(freePhotosBean.questionDesc)
        submitTimeTv.text = XUtils.date().getDateStrByDate(freePhotosBean.createTime, "yyyy-MM-dd")
        submitLocTv.text = XUtils.string().getNotNullValue(freePhotosBean.questionAddress)

        zpTimeTv.text = XUtils.date().getDateStrByDate(freePhotosBean.createTime, "yyyy-MM-dd HH:mm")
        if (fromWhere == 1) {// 是我发布的
            zpContentTv.text = "您的问题反馈单编号 ${freePhotosBean.questionCode} ,已反馈到相关部门，请耐心等待 ..."
            levelTv.visibility = View.GONE
        } else {
            zpContentTv.text = "请${XUtils.string().getNotNullValue(freePhotosBean.lootDeptName, "相关部门")}及时处理本次事项！"

            // 紧急程度
            levelTv.visibility = View.VISIBLE
            when (freePhotosBean.worryLevel.code) {
                "PT" -> {
                    levelTv.text = "一般"
                    levelTv.setBackgroundResource(R.color.level_3)
                }
                "JJ" -> {
                    levelTv.text = "紧急"
                    levelTv.setBackgroundResource(R.color.level_2)
                }
                "TJ" -> {
                    levelTv.text = "特急"
                    levelTv.setBackgroundResource(R.color.level_1)
                }
            }
        }

        if ("ING" == freePhotosBean.status.code) {
            // 显示图片
            showFileView(freePhotosBean.fileList, false)
            // 显示样式 红点和划线
            zpItem.setBackgroundResource(R.color.white)
            zpPoint.setBackgroundResource(R.drawable.shape_point_flow_doing)
            if (freePhotosBean.claimUser == LocalCache.getLoginUserId() && fromWhere == 2) {
                bottomView.visibility = View.VISIBLE
                submitBtn.setOnClickListener {
                    XUtils.intent().openActivity(this@FreePhotosDetailActivity, FreePhotosAddDoingActivity::class.java)
                        .putStringExtra("id", freePhotosBean.id)
                        .start()
                }
            } else {
                bottomView.visibility = View.GONE
            }
        } else { // 已完成
            bottomView.visibility = View.GONE
            // 设置上一级信息
            zpItem.setBackgroundResource(R.color.page_tip)
            zpPoint.setBackgroundResource(R.drawable.shape_point_flow_done)

            doneItem.visibility = View.VISIBLE
            doneContentTv.text = XUtils.string().getNotNullValue(freePhotosBean.summary)
            doneTimeTv.text = XUtils.date().getDateStrByDate(freePhotosBean.doneTime, "yyyy-MM-dd HH:mm")
            // 显示图片
            showFileView(freePhotosBean.fileList, true)
        }
    }

    // 显示图片
    private fun showFileView(fileList: ArrayList<FreePhotosFileBean>?, hasFinish: Boolean) {
        val upFileList = ArrayList<String>()
        val doneFileList = ArrayList<String>()

        if (fileList != null && fileList.size != 0) {
            for (fileBean in fileList) {
                if (fileBean.scourceType == null) {
                    continue
                }
                if ("SB" == fileBean.scourceType.code) {
                    if (XUtils.string().isNotEmpty(fileBean.imgUrl)) {
                        upFileList.add(fileBean.imgUrl)
                    }
                } else {
                    if (XUtils.string().isNotEmpty(fileBean.imgUrl)) {
                        doneFileList.add(fileBean.imgUrl)
                    }
                }
            }
        }

        if (upFileList.size > 0) {
            val nineGridAdapter = FreePhotosNineGridAdapter(this, upFileList)
            upFileNineGridView.nineGridAdapter = nineGridAdapter

            nineGridAdapter.setOnItemClickListener { position, urlOrFilePathList ->
                PhotoPreviewActivity.start(this, position, urlOrFilePathList)
            }

            contentImgItem.visibility = View.VISIBLE
        } else {
            contentImgItem.visibility = View.GONE
        }

        if (!hasFinish) {
            doneImgItem.visibility = View.GONE
        } else {
            doneImgItem.visibility = View.VISIBLE

            if (doneFileList.size > 0) {
                val nineGridAdapter = FreePhotosNineGridAdapter(this, doneFileList)
                done9GridView.nineGridAdapter = nineGridAdapter

                nineGridAdapter.setOnItemClickListener { position, urlOrFilePathList ->
                    PhotoPreviewActivity.start(this, position, urlOrFilePathList)
                }

                doneImgItem.visibility = View.VISIBLE
            } else {
                doneImgItem.visibility = View.GONE
            }
        }
    }
}